package com.javacoo.junit.generator.internal.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.javacoo.junit.generator.constant.Constants;

/**
 * 文件处理工具类
 * <li></li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2021/1/22 8:40
 */
public class FileUtility {
    private FileUtility() {
        super();
    }

    /**
     * 备份上一次文件
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2021/1/21 15:51
     * @param sourceFilePath: 原来文件路径
     * @return: void
     */
    public static void backup(String sourceFilePath) throws IOException {
        if(!StringUtility.stringHasValue(sourceFilePath)){
            return;
        }
        String targerPath = new StringBuilder().append(sourceFilePath.substring(0,sourceFilePath.lastIndexOf("/")+1)).append(
            Constants.BACK_UP_DIR).append(sourceFilePath.substring(sourceFilePath.lastIndexOf("/"))).append(Constants.BACK_UP_POSTFIX).toString();
        FileUtils.copyFile(new File(sourceFilePath),new File(targerPath));
    }
    /**
     * 将字符串写入文件
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2021/1/22 8:44
     * @param outFile: 目标文件路径
     * @param fileString: 字符串
     * @return: void
     */
    public static void writeStringToFile(String outFile,String fileString) throws IOException {
        FileUtils.writeStringToFile(new File(outFile), fileString,"UTF-8");
    }
}
